import { Style, Circle, Fill, Stroke, Icon, Text } from "ol/style";
import { StyleFunction } from "ol/style/Style";
import { Color } from "ol/color";
import { ColorLike, PatternDescriptor } from "ol/colorlike";
import markBlue from "../assets/images/mark_blue.png";
import { createStyle } from "../create/createStyle";
import Feature, { FeatureLike } from "ol/Feature";
import { resolution2scale } from "../utils/sizeFit";

// export function createLineStyle(styleOptions) {}

// type StyleFunctionParameters = Parameters<StyleFunction>;
// type StyleFunctionReturnType = ReturnType<StyleFunction>;
// type StyleFunctionType = [...StyleFunctionParameters, Style];

export type LineStyleOptions = {
  // scale?: number;
  color?: Color | ColorLike;
  sizeFit?: boolean;
  styleFunction?: (
    feature: FeatureLike,
    resolution: number,
    style: Style
  ) => ReturnType<StyleFunction>;
};

export function createLineStyle(styleOptions?: LineStyleOptions) {
  const stroke = new Stroke({
    width: 2,
    color: styleOptions?.color || "#000",
  });
  let style = createStyle({
    stroke,
  });

  if (styleOptions?.styleFunction) {
    return (feature: FeatureLike, resolution: number) =>
      styleOptions.styleFunction!(feature, resolution, style);
  }

  if (styleOptions?.sizeFit) {
    return (feature: FeatureLike, resolution: number) => {
      //map.getView().getZoomForResolution(resolution);

      const scale = resolution2scale(resolution);

      let width = feature.get("strokWidth");
      if (width === undefined) {
        width = style.getStroke()?.getWidth();
        (feature as Feature).set("strokWidth", width);
      }

      const item = feature.get("data");
      const color = item.color;
      color && style.getStroke()?.setColor(color);

      style.getStroke()?.setWidth(+(scale * +width).toFixed(2));

      return style;
    };
  }

  return style;
}
